/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Analyse;


/*import infinity.seed.InfinitySeed;*/
/**
 *
 * @author Arthur
 */
public class CellAnalyse {
    
    private int x_size;
    private int y_size;
    private int[][] array;
    private int[][] transiArray;

    public void setX_size(int x_size) {
        this.x_size = x_size;
    }

    public void setY_size(int y_size) {
        this.y_size = y_size;
    }

    public int[][] getArray() {
        return array;
    }
    
    public void setArray(int[][] array) {
        this.array = array;
    }

    public void setTransiArray(int[][] transiArray) {
        this.transiArray = transiArray;
    }

    
    @SuppressWarnings("empty-statement")
    public int[][] startCellAnalyse(int[][] array, int[][] transiArray, int x_size, int y_size) // x size = largeur du tableau  || y size = hauteur du tableau
    {
        //<editor-fold desc="Boucle d'analyse">
        for ( int a = 0 ; a <= x_size-1 ; a++ )
        {
            
            for (int b = 0 ; b <= y_size-1 ; b ++)
            {
                int getState1=0;
                int getState2=0;
                int getState3=0;
                int getState4=0;
                int getState5=0;
                int getState6=0;
                int getState7=0;
                int getState8=0;
                
                //<editor-fold desc="Variables propres à chaque cellule">
                
                int neighbourCount=0;
                int bushCount=0;
                int treeCount=0;
                int inFireCount=0;
                int infectedCount=0;
                int actualState = array[a][b];
                int lower = 1; // minimum pour la fonction Math.random()
                int higher = 100; // max_sizeimum pour la fonction Math.random()
                int random; // valeur du random
                
                //</editor-fold>
                
                //<editor-fold desc="Gestion des cases en bord de tableau">
                
                
                if ( a == 0 )
                {
                    if ( b == 0 )
                    {
                        getState5 = array[a+1][b];
                        getState7 = array[a][b+1];
                        getState8 = array[a+1][b+1];
                    }
                    else if ( b == y_size-1 )
                    {
                        getState2 = array[a][b-1];
                        getState3 = array[a+1][b-1];
                        getState5 = array[a+1][b];
                    }
                    else
                    {
                        getState2 = array[a][b-1];
                        getState3 = array[a+1][b-1];
                        getState5 = array[a+1][b];
                        getState7 = array[a][b+1];
                        getState8 = array[a+1][b+1];
                    }
                }
                else if ( a == x_size-1)
                {
                    if ( b == 0 )
                    {
                        getState4 = array[a-1][b];
                        getState6 = array[a-1][b+1];
                        getState7 = array[a][b+1];
                    }
                    else if ( b == y_size-1 )
                    {
                        getState1 = array[a-1][b-1];
                        getState2 = array[a][b-1];
                        getState4 = array[a-1][b];
                    }
                    else
                    {
                        getState1 = array[a-1][b-1];
                        getState2 = array[a][b-1];
                        getState4 = array[a-1][b];
                        getState6 = array[a-1][b+1];
                        getState7 = array[a][b+1];
                    }
                }
                else if ( a > 0 && a < x_size-1 )
                {
                    if ( b == 0 )
                    {
                        getState4 = array[a-1][b];
                        getState5 = array[a+1][b];
                        getState6 = array[a-1][b+1];
                        getState7 = array[a][b+1];
                        getState8 = array[a+1][b+1];
                    }
                    else if ( b == y_size-1 )
                    {
                        getState1 = array[a-1][b-1];
                        getState2 = array[a][b-1];
                        getState3 = array[a+1][b-1];
                        getState4 = array[a-1][b];
                        getState5 = array[a+1][b];
                    }
                    else
                    {
                        getState1 = array[a-1][b-1];
                        getState2 = array[a][b-1];
                        getState3 = array[a+1][b-1];
                        getState4 = array[a-1][b];
                        getState5 = array[a+1][b];
                        getState6 = array[a-1][b+1];
                        getState7 = array[a][b+1];
                        getState8 = array[a+1][b+1];
                    }
                }
                
                //</editor-fold>
                    
                //<editor-fold desc="Switch pour getState1">
                
                switch(getState1)
                {
                    case 0 : break;
                    case 1 : neighbourCount ++; break;
                    case 2 : neighbourCount ++; bushCount++; break;
                    case 3 : neighbourCount ++; treeCount++; break;
                    case 4 : neighbourCount ++; bushCount++; break;
                    case 5 :  break;
                    case 10 : neighbourCount ++; inFireCount++; break;
                    case 20 : neighbourCount ++; bushCount++; inFireCount++; break;
                    case 30 : neighbourCount ++; treeCount++; inFireCount++; break;
                    case 40 : neighbourCount ++; bushCount++; inFireCount++; break;
                    case 11 : neighbourCount ++; break;
                    case 21 : neighbourCount ++; bushCount++; break;
                    case 31 : neighbourCount ++; treeCount++; break;
                    case 41 : neighbourCount ++; bushCount++; break;
                        
                }
                
                
                //</editor-fold>
                
                //<editor-fold desc="Switch pour getState2">
                
                switch(getState2)
                {
                    case 0 : break;
                    case 1 : neighbourCount ++; break;
                    case 2 : neighbourCount ++; bushCount++; break;
                    case 3 : neighbourCount ++; treeCount++; break;
                    case 4 : neighbourCount ++; bushCount++; break;
                    case 5 :  break;
                    case 10 : neighbourCount ++; inFireCount++; break;
                    case 20 : neighbourCount ++; bushCount++; inFireCount++; break;
                    case 30 : neighbourCount ++; treeCount++; inFireCount++; break;
                    case 40 : neighbourCount ++; bushCount++; inFireCount++; break;
                    case 11 : neighbourCount ++; infectedCount++; break;
                    case 21 : neighbourCount ++; bushCount++; infectedCount++; break;
                    case 31 : neighbourCount ++; treeCount++; infectedCount++; break;
                    case 41 : neighbourCount ++; bushCount++; infectedCount++; break;
                        
                }
                
                
                //</editor-fold>
                
                //<editor-fold desc="Switch pour getState3">
                
                switch(getState3)
                {
                    case 0 : break;
                    case 1 : neighbourCount ++; break;
                    case 2 : neighbourCount ++; bushCount++; break;
                    case 3 : neighbourCount ++; treeCount++; break;
                    case 4 : neighbourCount ++; bushCount++; break;
                    case 5 :  break;
                    case 10 : neighbourCount ++; inFireCount++; break;
                    case 20 : neighbourCount ++; bushCount++; inFireCount++; break;
                    case 30 : neighbourCount ++; treeCount++; inFireCount++; break;
                    case 40 : neighbourCount ++; bushCount++; inFireCount++; break;
                    case 11 : neighbourCount ++; break;
                    case 21 : neighbourCount ++; bushCount++; break;
                    case 31 : neighbourCount ++; treeCount++; break;
                    case 41 : neighbourCount ++; bushCount++; break;
                        
                }
                
                
                //</editor-fold>
                
                //<editor-fold desc="Switch pour getState4">
                
                switch(getState4)
                {
                    case 0 : break;
                    case 1 : neighbourCount ++; break;
                    case 2 : neighbourCount ++; bushCount++; break;
                    case 3 : neighbourCount ++; treeCount++; break;
                    case 4 : neighbourCount ++; bushCount++; break;
                    case 5 :  break;
                    case 10 : neighbourCount ++; inFireCount++; break;
                    case 20 : neighbourCount ++; bushCount++; inFireCount++; break;
                    case 30 : neighbourCount ++; treeCount++; inFireCount++; break;
                    case 40 : neighbourCount ++; bushCount++; inFireCount++; break;
                    case 11 : neighbourCount ++; infectedCount++; break;
                    case 21 : neighbourCount ++; bushCount++; infectedCount++; break;
                    case 31 : neighbourCount ++; treeCount++; infectedCount++; break;
                    case 41 : neighbourCount ++; bushCount++; infectedCount++; break;
                        
                }
                
                
                //</editor-fold>
                
                //<editor-fold desc="Switch pour getState5">
                
                switch(getState5)
                {
                    case 0 : break;
                    case 1 : neighbourCount ++; break;
                    case 2 : neighbourCount ++; bushCount++; break;
                    case 3 : neighbourCount ++; treeCount++; break;
                    case 4 : neighbourCount ++; bushCount++; break;
                    case 5 :  break;
                    case 10 : neighbourCount ++; inFireCount++; break;
                    case 20 : neighbourCount ++; bushCount++; inFireCount++; break;
                    case 30 : neighbourCount ++; treeCount++; inFireCount++; break;
                    case 40 : neighbourCount ++; bushCount++; inFireCount++; break;
                    case 11 : neighbourCount ++; infectedCount++; break;
                    case 21 : neighbourCount ++; bushCount++; infectedCount++; break;
                    case 31 : neighbourCount ++; treeCount++; infectedCount++; break;
                    case 41 : neighbourCount ++; bushCount++; infectedCount++; break;
                        
                }
                
                
                //</editor-fold>
                
                //<editor-fold desc="Switch pour getState6">
                
                switch(getState6)
                {
                    case 0 : break;
                    case 1 : neighbourCount ++; break;
                    case 2 : neighbourCount ++; bushCount++; break;
                    case 3 : neighbourCount ++; treeCount++; break;
                    case 4 : neighbourCount ++; bushCount++; break;
                    case 5 :  break;
                    case 10 : neighbourCount ++; inFireCount++; break;
                    case 20 : neighbourCount ++; bushCount++; inFireCount++; break;
                    case 30 : neighbourCount ++; treeCount++; inFireCount++; break;
                    case 40 : neighbourCount ++; bushCount++; inFireCount++; break;
                    case 11 : neighbourCount ++; break;
                    case 21 : neighbourCount ++; bushCount++; break;
                    case 31 : neighbourCount ++; treeCount++; break;
                    case 41 : neighbourCount ++; bushCount++; break;
                        
                }
                
                
                //</editor-fold>
                
                //<editor-fold desc="Switch pour getState7">
                
                switch(getState7)
                {
                    case 0 : break;
                    case 1 : neighbourCount ++; break;
                    case 2 : neighbourCount ++; bushCount++; break;
                    case 3 : neighbourCount ++; treeCount++; break;
                    case 4 : neighbourCount ++; bushCount++; break;
                    case 5 :  break;
                    case 10 : neighbourCount ++; inFireCount++; break;
                    case 20 : neighbourCount ++; bushCount++; inFireCount++; break;
                    case 30 : neighbourCount ++; treeCount++; inFireCount++; break;
                    case 40 : neighbourCount ++; bushCount++; inFireCount++; break;
                    case 11 : neighbourCount ++; infectedCount++; break;
                    case 21 : neighbourCount ++; bushCount++; infectedCount++; break;
                    case 31 : neighbourCount ++; treeCount++; infectedCount++; break;
                    case 41 : neighbourCount ++; bushCount++; infectedCount++; break;
                        
                }
                
                
                //</editor-fold>
                
                //<editor-fold desc="Switch pour getState8">
                
                switch(getState8)
                {
                    case 0 : break; 
                    case 1 : neighbourCount ++; break; 
                    case 2 : neighbourCount ++; bushCount++; break; 
                    case 3 : neighbourCount ++; treeCount++; break; 
                    case 4 : neighbourCount ++; bushCount++; break; 
                    case 5 :  break; 
                    case 10 : neighbourCount ++; inFireCount++; break; 
                    case 20 : neighbourCount ++; bushCount++; inFireCount++; break; 
                    case 30 : neighbourCount ++; treeCount++; inFireCount++; break; 
                    case 40 : neighbourCount ++; bushCount++; inFireCount++; break; 
                    case 11 : neighbourCount ++; break; 
                    case 21 : neighbourCount ++; bushCount++; break;
                    case 31 : neighbourCount ++; treeCount++; break; 
                    case 41 : neighbourCount ++; bushCount++; break; 
                        
                }
                
                
                //</editor-fold>
                
                //<editor-fold desc="gestion de l'évolution de la vie">
                
                
                // LISTE DES DIFFERENTS ETATS : 0 = VIDE 
                // 1 = SEED // 2 = BUSH // 3 = TREE // 4 = BUSH+1 // 5 = IN ASHES 
                // 10 = SEED IN FIRE  // 20 = BUSH IN FIRE // 30 = TREE IN FIRE // 40 = BUSH +1 IN FIRE
                // 11 = SEED INFECTED // 21 = BUSH INFECTED // 31 = TREE INFECTED // 41 = BUSH+1 INFECTED
                
                if ( actualState == 0 )
                {
                    if ( treeCount >= 2 || bushCount >= 3 || (treeCount == 1 && bushCount == 2  ) )
                    {
                        this.transiArray[a][b] = 1;
                    }
                    
                    else
                    {
                        this.transiArray[a][b] = actualState;
                    }
                }
                
                else if ( actualState == 1 )
                {
                    if ( inFireCount > 0 )
                    {
                        random = (int)(Math.random() * (higher-lower)) + lower;
                        if ( random <= 25 )
                        {
                            this.transiArray[a][b] = 10;
                        }
                        else
                        {
                            this.transiArray[a][b] = actualState;
                        }
                        
                
                    }
                    else if ( infectedCount > 0 )
                    {
                        random = (int)(Math.random() * (higher-lower)) +lower;
                        if ( random <= 75 )
                        {
                            this.transiArray[a][b] = 11;
                        }
                        else
                        {
                            this.transiArray[a][b] = actualState;
                        }
                    }
                    else
                    {
                        if ( (treeCount + bushCount)  <= 3 )
                        {
                            this.transiArray[a][b] = 2;
                        }

                        else
                        {
                            this.transiArray[a][b] = actualState;
                        }
                    }
                }
                
                else if ( actualState == 2 )
                {
                    if ( inFireCount > 0 )
                    {
                        random = (int)(Math.random() * (higher-lower)) + lower;
                        if ( random <=50 )
                        {
                            this.transiArray[a][b] = 20;
                        }
                        
                        else 
                        {
                            this.transiArray[a][b] = actualState;
                        }
                    }
                    
                    else if ( infectedCount > 0 )
                    {
                        random = (int)(Math.random() * (higher-lower)) +lower;
                    if (random <= 50 )
                    {
                        this.transiArray[a][b] = 21;
                    }
                    
                    else
                    {
                        this.transiArray[a][b] = actualState;
                    }
                    }
                    else
                    {
                        this.transiArray[a][b] = 4;
                    }
                }
                
                else if ( actualState == 3 )
                {
                    if ( inFireCount > 0 )
                    {
                        random = (int)(Math.random() * (higher-lower)) + lower;
                        if ( random <= 75 )
                        {
                            this.transiArray[a][b] = 30;
                        }
                        
                        else
                        {
                            this.transiArray[a][b] = actualState;
                        }
                    }
                    else if ( infectedCount > 0 )
                    {
                        random = (int)(Math.random() * (higher-lower)) +lower;
                    if (random <= 25)
                    {
                        this.transiArray[a][b] = 31;
                    }
                    
                    else
                    {
                        this.transiArray[a][b] = actualState ;
                    }
                    }
                    else
                    {
                        this.transiArray[a][b] = actualState;
                    }
                }
                
                else if ( actualState == 4 )
                {
                    if ( inFireCount > 0 )
                    {
                        random = (int)(Math.random() * (higher-lower)) + lower;
                        if ( random <=50 )
                        {
                           this.transiArray[a][b] = 40;
                        }
                        
                        else 
                        {
                            this.transiArray[a][b] = actualState;
                        }
                    }
                    else if ( infectedCount > 0 )
                    {
                        random = (int)(Math.random() * (higher-lower)) +lower;
                    if (random <= 50 )
                    {
                        this.transiArray[a][b] = 41;
                    }
                    
                    else
                    {
                        this.transiArray[a][b] = actualState;
                    }
                    }
                    else
                    {
                        this.transiArray[a][b] = 3;
                    }
                }
                
                else if ( actualState == 10 || actualState == 20 || actualState == 30 || actualState == 40 )
                    {
                        this.transiArray[a][b] = 5;
                    }
                    
                else if ( actualState == 5 )
                    {
                        this.transiArray[a][b] = 0;
                    }
                else if ( actualState == 11 || actualState == 21 || actualState == 31 || actualState == 41 )
                {
                    this.transiArray[a][b] = 0;
                }
                //</editor-fold>
                
           }
        }
       
        //</editor-fold>
        
        return this.transiArray;
    }

    public void startSimulation() throws InterruptedException
    {
            this.startCellAnalyse(this.array, this.transiArray, this.x_size, this.y_size);// prend en paramètres x_size, y_size et array[][]
    }
    
    @SuppressWarnings("empty-statement")
    public int[][] getNewArray(int[][] array, int[][] transiArray, int x_size, int y_size) // METHODE DE MODIFICATION DU ARRAY POUR LE RE-AFFICHER
    {
        for ( int a = 0 ; a <= x_size-1 ; a ++)
        {
            for ( int b = 0 ; b <= y_size-1 ; b ++ )
            {
                
                this.array[a][b] = this.transiArray[a][b];
            }
        }
        return this.array;
    }
}
